var btn = document.querySelector(".btnadpt");
var tex = document.querySelector(".in1");   //用户
var sub = document.querySelector(".in2");   //密码

var userNew = getCookie("users") ? JSON.parse(getCookie("users")) : [];
btn.onclick = function () {
    if(userNew.length==0){
     this.innerHTML="用户不存在，请注册"
     console.log(1);
    }
    else{
        var index = userNew.findIndex((el, idx) => {
            return el.personName == tex.value;
        })
        console.log(index)
        if (index != -1) {
            if (userNew[index].personpwd == sub.value) {
                userNew[index].state = 1;
                setCookie("users", JSON.stringify(userNew), 5)
                this.innerHTML = "登陆成功，即将进入首页...";
                setTimeout(function () {
                    location.href="./index.html";
                }, 2000)
            } else {
                this.innerHTML = "密码错误";
            }
        }
        else {
            this.innerHTML = "用户不存在";
        }
    }   
}
// 格式化cookie
function setCookie(key, value, timing) {
    var date = new Date();
    date.setDate(date.getDate() + timing);
    document.cookie = key + "=" + value + ";expires=" + date;
}
function getCookie(key) {
    var arr = document.cookie.split("; ");
    for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split("=");
        if (key === arr2[0]) {
            return arr2[1]
        }
    }
}
